export default {
    state: {
        isselect: false, //是否刷新
        ListAllData: [{
            id: 1,
            Year: "2021",
            title: "2021",
            term: "2020/01/01 - 2021/05/06",
            RuleList: [{
                id: 1,
                name: "高新技术企业加分",
                detail: "高新技术企业加3分",
                type: 1, // 0分数 1比例
                situation: 0, //0增 ；1减
                point: 3, //分值
                remarks: "高新技术研发科认证"
            }]
        },  ],
        ListData: [],
        selectionList: [], // 当前选择的结果
        dataCount: 0, // 总条数
        pageSize: 10, // 每页显示多少条
        pageCurrent: 1, // 当前显示第几页
        pageShowsizer: true,
        pageShowTotal: true,
        pageShowElevator: true,
        pageSizeOpts: [5, 10, 20, 30, 40],
    },
    getters: {

    },
    actions: {
        addRuleData({
            commit,
            state
        }, data) {
            console.log("__data__", data);
            return new Promise((reslove) => {
                let isHaveYear = false;
                state.ListAllData.map(ele => {
                    console.log(ele)
                    if (ele.Year === data.Year) {
                        isHaveYear = true;
                        return true
                    } else {
                        return false;
                    }
                })
                if (isHaveYear === true) {
                    reslove({
                        code: 3003,
                        msg: "评价年度重复!"
                    })
                } else {
                    state.ListAllData.unshift({
                        id: state.ListAllData.length + 1,
                        Year: data.Year,
                        title: data.Year,
                        term: data.term,
                        RuleList:[],
                    })
                    reslove({
                        code: 2000,
                        msg: "新增成功!"
                    })
                }
            })
        },
        editRuleData({
            commit,
            state
        }, data) {
            console.log("_data__", data)
            return new Promise((reslove) => {
                let findEditIndex = state.ListAllData.findIndex(ele => {
                    return ele.id == data.id
                })
                if (findEditIndex == -1) {
                    reslove({
                        code: 3003,
                        msg: "修改失败!"
                    })
                } else {
                    state.ListAllData[findEditIndex].term = data.term;
                    reslove({
                        code: 2000,
                        msg: "修改成功!"
                    })
                }
            })

        },
        getRuleListData({
            commit,
            state
        }, isgetAll = false) {
            return new Promise((reslove) => {
                if (isgetAll == true) {
                    state.ListData = state.ListAllData;
                } else {
                    state.dataCount = state.ListAllData.length;
                    if (state.ListAllData.length > 0) {
                        state.ListData = state.ListAllData.slice(
                            (state.pageCurrent * state.pageSize) - state.pageSize, state.pageCurrent * state.pageSize
                        )
                    }
                }
                reslove(200);
            })

        },
        addRuleList({
            commit,
            state
        }, {
            fatherId,
            data
        }) {

            return new Promise((reslove) => {
                let findEditIndex = state.ListAllData.findIndex(ele => {
                    return ele.id == fatherId;
                })
                console.log("__ findEditIndex __", findEditIndex, "___");
                if (findEditIndex == -1) {
                    reslove({
                        code: 3003,
                        msg: "增加失败!"
                    })
                } else {
                    console.log("___", state.ListAllData[findEditIndex].RuleList.length);
                    state.ListAllData[findEditIndex].RuleList.unshift({
                        id: state.ListAllData[findEditIndex].RuleList.length,
                        name: data.name,
                        detail: data.detail,
                        type: data.type, // 0分数 1比例
                        situation: data.situation, //0增 ；1减
                        point: data.point, //分值
                        remarks: data.remarks
                    })
                    reslove({
                        code: 2000,
                        msg: "增加成功!"
                    })
                }
            })
        },
        editRuleList({
            commit,
            state
        }, {
            fatherId,
            id,
            data
        }) {
            return new Promise((reslove) => {
                let findEditIndex = state.ListAllData.findIndex(ele => {
                    return ele.id == fatherId;
                })
                if (findEditIndex == -1) {
                    reslove({
                        code: 3003,
                        msg: "增加失败!"
                    })
                } else {
                    let findEditIndexChild = state.ListAllData[findEditIndex].RuleList.findIndex(ele => {
                        return ele.id == id;
                    })
                    if (findEditIndexChild == -1) {
                        reslove({
                            code: 3003,
                            msg: "增加失败!"
                        })
                    } else {
                        state.ListAllData[findEditIndex].RuleList[findEditIndexChild].name = data.name;
                        state.ListAllData[findEditIndex].RuleList[findEditIndexChild].detail = data.detail;
                        state.ListAllData[findEditIndex].RuleList[findEditIndexChild].type = data.type;
                        state.ListAllData[findEditIndex].RuleList[findEditIndexChild].situation = data.situation;
                        state.ListAllData[findEditIndex].RuleList[findEditIndexChild].point = data.point;
                        state.ListAllData[findEditIndex].RuleList[findEditIndexChild].remarks = data.remarks;
                    }
                    reslove({
                        code: 2000,
                        msg: "增加成功!"
                    })
                }
            })
        },
        deleteRuleList({
            commit,
            state
        }, {
            fatherId,
            id,
            data
        }) {
            return new Promise((reslove) => {
                let findEditIndex = state.ListAllData.findIndex(ele => {
                    return ele.id == fatherId;
                })
                console.log("__findEditIndex_",findEditIndex)

                if (findEditIndex == -1) {
                    reslove({
                        code: 3003,
                        msg: "增加失败!"
                    })
                } else {
                    let findEditIndexChild = state.ListAllData[findEditIndex].RuleList.findIndex(ele => {
                        return ele.id == id;
                    })
                    console.log("__findEditIndexChild_",findEditIndexChild)
                    if (findEditIndexChild == -1) {
                        reslove({
                            code: 3003,
                            msg: "增加失败!"
                        })
                    } else {
                        console.log("__findEditIndex_",findEditIndex,"___findEditIndexChild",findEditIndexChild)
                        state.ListAllData[findEditIndex].RuleList.splice(findEditIndexChild, 1)
                    }
                    reslove({
                        code: 2000,
                        msg: "增加成功!"
                    })
                }
            })
        }
    },
    mutations: {
        setRule(
            state, {
                isselect,
                ListAllData,
                ListData,
                dataCount,
                pageSize,
                pageCurrent,
            },
        ) {
            if (isselect != undefined)
                state.isselect = isselect;
            if (ListAllData != undefined)
                state.ListAllData = ListAllData;
            if (ListData != undefined)
                state.ListData = ListData;
            if (dataCount != undefined)
                state.dataCount = dataCount;
            if (pageSize != undefined)
                state.pageSize = pageSize;
            if (pageCurrent != undefined)
                state.pageCurrent = pageCurrent;

        }
    }

}